package com.lineage.server.timecontroller.event;

import com.lineage.server.datatables.lock.DwarfShopReading;
import com.lineage.server.templates.L1ShopS;
import com.lineage.server.thread.GeneralThreadPool;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ShopXTime extends TimerTask {
  private static final Log _log = LogFactory.getLog(ShopXTime.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 3600000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 3600000L, 3600000L);
  }
  
  public void run() {
    try {
      Timestamp overTime = new Timestamp(System.currentTimeMillis());
      HashMap<Integer, L1ShopS> allShopS = DwarfShopReading.get().allShopS();
      Iterator<L1ShopS> iterator = allShopS.values().iterator();
      while (iterator.hasNext()) {
        L1ShopS shopS = iterator.next();
        if (shopS.get_end() == 0 && overTime.after(shopS.get_overtime())) {
          shopS.set_end(3);
          shopS.set_item(null);
          DwarfShopReading.get().updateShopS(shopS);
        } 
        Thread.sleep(1L);
      } 
      allShopS.clear();
    } catch (Exception e) {
      _log.error("托售物件时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      ShopXTime timerTask = new ShopXTime();
      timerTask.start();
    } 
  }
}
